﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace MO.ModelingClient
{
    public partial class ReportForm : Form
    {
        public ReportForm()
        {
            InitializeComponent();
        }
        private void ReportForm_Shown(object sender, EventArgs e)
        {
            List<QueryTemplate> result = new List<QueryTemplate>();
            DataTable table = DbOperation.DB.ExecuteDataTable("select * from Query_Template order by QT_Order");
            DataTable colTable = DbOperation.DB.ExecuteDataTable("select * from Query_Column order by QC_Order");
            Dictionary<string, QueryTemplate> dict = new Dictionary<string, QueryTemplate>();
            foreach (DataRow row in table.Rows)
            {
                QueryTemplate temp = new QueryTemplate(row);
                dict[temp.Code] = temp;
                TreeNode node = new TreeNode();
                node.Text = temp.Name;
                node.Tag = temp;
                treeView.Nodes.Add(node);
            }
            foreach (DataRow row in colTable.Rows)
            {
                QueryColumn col = new QueryColumn(row);
                col.FieldDefine = DataManager.FindFieldByFieldCode(col.FieldCode);
                if (dict.ContainsKey(col.TempCode))
                    dict[col.TempCode].Columns.Add(col);
            }
        }
        private void buttonQuery_Click(object sender, EventArgs e)
        {

        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            QueryTemplate temp = (QueryTemplate)e.Node.Tag;
            List<string> cols = new List<string>();
            HashSet<string> hashTable = new HashSet<string>();
            foreach (var col in temp.Columns)
            {
                if (!string.IsNullOrEmpty(col.Format))
                {
                    cols.Add(col.Format.Replace("[value]", col.FieldCode) + " as " + col.Name);
                }
                else
                {
                    cols.Add(col.FieldCode + " as " + col.Name);
                }
                if (!hashTable.Contains(col.FieldDefine.EntityCode))
                    hashTable.Add(col.FieldDefine.EntityCode);
            }
            string whereStr = "";
            if (!string.IsNullOrEmpty(temp.QueryCondition))
                whereStr = " where " + temp.QueryCondition;
            string orderStr = "";
            if (!string.IsNullOrEmpty(temp.OrderCondition))
                orderStr = " order by " + temp.OrderCondition;

            string sqlStr = string.Format("select {0} from {1} {2} {3}", string.Join(",", cols.ToArray()), string.Join(",", hashTable.ToArray()), whereStr, orderStr);
            dataGridView.DataSource = DbOperation.DB.ExecuteDataTable(sqlStr);
        }


    }
    public class QueryTemplate
    {
        public string Code;
        public string Name;
        public string Description;
        public string QueryCondition;
        public string OrderCondition;
        public bool TreeShow;
        public List<QueryColumn> Columns = new List<QueryColumn>();
        public QueryTemplate(DataRow row)
        {
            this.Code = row["QT_Code"].ToString();
            this.Name = row["QT_Name"].ToString();
            this.Description = row["QT_Description"] as string;
            this.QueryCondition = row["QT_Query_Condition"] as string;
            this.OrderCondition = row["QT_Order_Condition"] as string;
            this.TreeShow = Convert.ToBoolean(row["QT_Tree_Show"]);
        }
    }
    public class QueryColumn
    {
        public string Code;
        public string Name;
        public string TempCode;
        public string FieldCode;
        public string Format;
        public int Order;
        public FieldDefine FieldDefine;
        public QueryColumn(DataRow row)
        {
            this.Code = row["QC_Code"].ToString();
            this.Name = row["QC_Name"].ToString();
            this.TempCode = row["QT_Code"].ToString();
            this.FieldCode = row["FD_Code"] as string;
            this.Format = row["QC_Format"] as string;
            this.Order = Convert.ToInt32(row["QC_Order"]);
        }
    }
}
